import cy_operate
import time
import fcntl
import json

base_path = './logs/'

# 今天
def toDay():

    lsat_send_time = cy_operate.getCurrentTime()
    start_now = float(lsat_send_time) / 1000
    start_timeArray = time.localtime(start_now)
    start_otherStyleTime = time.strftime("%Y-%m-%d", start_timeArray)

    return start_otherStyleTime

def inLog(string):
    n_string = string + '\n'

    # 按照日期进行区分开来
    log_name = base_path + toDay() + '_log.txt'

    f = open(log_name, 'a+')

    fcntl.flock(f.fileno(), fcntl.LOCK_EX)

    f.write(n_string)
    f.close()

def inLog_k(string):
    n_string = string + '\n'

    # 按照日期进行区分开来
    log_name = base_path + toDay() + '_k_log.txt'

    f = open(log_name, 'a+')

    fcntl.flock(f.fileno(), fcntl.LOCK_EX)

    f.write(n_string)
    f.close()

def inLog_more(string):
    n_string = string + '\n'
    
    # 按照日期进行区分开来
    log_name = base_path + toDay() + '_open_more_log.txt'

    f = open(log_name, 'a+')

    fcntl.flock(f.fileno(), fcntl.LOCK_EX)

    f.write(n_string)
    f.close()

def inLog_less(string):
    n_string = string + '\n'

    # 按照日期进行区分开来
    log_name = base_path + toDay() + '_open_less_log.txt'

    f = open(log_name, 'a+')

    fcntl.flock(f.fileno(), fcntl.LOCK_EX)

    f.write(n_string)
    f.close()

def inLog_more_sell(string):
    with open(base_path + 'open_more_sell_orders_log.json', 'w', encoding='utf-8') as fp:
        json.dump(string, fp, ensure_ascii=False) # 转为json字符并且存入本地文件中

def inLog_less_sell(string):
    with open(base_path + 'open_less_sell_orders_log.json', 'w', encoding='utf-8') as fp:
        json.dump(string, fp, ensure_ascii=False) # 转为json字符并且存入本地文件中

def inLog_profit(string):
    n_string = string + '\n'

    f = open(base_path + 'log_profit.txt', 'a')

    fcntl.flock(f.fileno(), fcntl.LOCK_EX)

    f.write(n_string)
    f.close()

def printAndInLog_k(string):
    
    lsat_send_time = cy_operate.getCurrentTime()

    start_now = float(lsat_send_time) / 1000
    start_timeArray = time.localtime(start_now)
    start_otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", start_timeArray)
    
    string = start_otherStyleTime + ' ' + string

    print_string = "\033[1;35m" + string + "\033[0m"
    # print(print_string)
    inLog_k(string)

def printAndInLog(string):

    lsat_send_time = cy_operate.getCurrentTime()

    start_now = float(lsat_send_time) / 1000
    start_timeArray = time.localtime(start_now)
    start_otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", start_timeArray)
    
    string = start_otherStyleTime + ' ' + string

    print_string = "\033[1;35m" + string + "\033[0m"
    # print(print_string)
    inLog(string)

def printAndInLog_more(string):

    lsat_send_time = cy_operate.getCurrentTime()

    start_now = float(lsat_send_time) / 1000
    start_timeArray = time.localtime(start_now)
    start_otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", start_timeArray)

    string = start_otherStyleTime + ' ' + string

    print_string = "\033[1;35m" + string + "\033[0m"
    # print(print_string)
    inLog_more(string)

def printAndInLog_less(string):

    lsat_send_time = cy_operate.getCurrentTime()

    start_now = float(lsat_send_time) / 1000
    start_timeArray = time.localtime(start_now)
    start_otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", start_timeArray)

    string = start_otherStyleTime + ' ' + string

    print_string = "\033[1;35m" + string + "\033[0m"
    # print(print_string)
    inLog_less(string)

def printAndInLog_more_sells(string):
    inLog_more_sell(string)

def printAndInLog_less_sells(string):
    inLog_less_sell(string)

def printAndInLog_profit(string):
    print_string = "\033[1;35m" + string + "\033[0m"
    # print(print_string)
    inLog_profit(string)
